package com.sec.android.app.myfiles.log;

import android.util.SparseArray;

import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * SamsungAnalytics interface class
 */
public class SamsungAnalyticsLog {
    private static final String sTrackingId = "404-399-1011025";
    private static final String sVersion = "0.1";
    private static SparseArray<String> mLoggingInfoEventIdMap = new SparseArray<>();
    private static SparseArray<String> mLoggingInfoPageIdMap = new SparseArray<>();
    private static int mEventCodeGenerator = 0;

    public enum ScreenMode {
        NORMAL,
        SELECTION_MODE
    }

    public enum Event {
        NONE(""),
        MORE_OPTIONS("More options"),
        SEARCH("Search"),
        TAB_RECENT_FILE("Tap %d th recent file"),
        EXTENDS_RECENT_FILES("Extends Recent files"),
        CATEGORY_IMAGES("Category_Images"),
        CATEGORY_AUDIO("Category_Audio"),
        CATEGORY_VIDEOS("Category_Videos"),
        CATEGORY_DOCUMENT("Category_Document"),
        CATEGORY_DOWNLOAD("Category_Download"),
        CATEGORY_INSTALLATION_FILES("Category_Installation files"),
        INTERNAL_STORAGE_USAGE("Internal storage usage"),
        INTERNAL_STORAGE("Internal storage"),
        SD_CARD("SD card"),
        USB_STORAGE("USB storage"),
        SHORTCUT("Shortcut"),
        LONG_PRESS("Long press"),
        SAMSUNG_CLOUD_DRIVE("Samsung Cloud Drive"),
        GOOGLE_DRIVE("Google Drive"),
        CLEAR_RECENT_FILE_HISTORY("Clear recent file"),
        GET_MORE_SPACE("Get more space"),
        MANAGE_SHORTCUTS("Manage shortcuts"),
        SETTINGS("Settings"),
        CONTACT_US("Contact us"),
        NAVIGATE_UP("Navigate up"),
        EDIT("Edit"),
        SHARE("Share"),
        SHARE_FILES_ONLY("Share files only"),
        SHARE_USING_WIFI_DIRECT("Share using Wi-Fi Direct"),
        SHARE_CANCEL("Cancel sharing"),
        SELECT_ALL("Select all"),
        VIEW_AS_GRID("View as grid"),
        VIEW_AS_LIST("View as list"),
        FILE_OPEN("File open"),
        ZOOM_IN("Zoom in"),
        ZOOM_OUT("Zoom out"),
        REMOVE("Remove"),
        ADD_SHORTCUT("Add shortcut"),
        SELECT_SHORTCUT_LOCATION("Select shortcut location"),
        DETAILS("Details"),
        FOLDER_NAVIGATION("Folder navigation"),
        FOLDER_OPEN("Folder open"),
        SORT_BY("Sort by"),
        DELETE("Delete"),
        MOVE("Move"),
        COPY("Copy"),
        RENAME("Rename"),
        RENAME_CANCEL_CHANGING_EXTESION("Cancel changing extension"),
        REPLACE("Replace"),
        SKIP("Skip"),
        APPLY_TO_ALL_ITEMS("Apply to all items"),
        CHANGE("Change extension"),
        ZIP("Zip"),
        UNZIP("Unzip"),
        UNZIP_TO_CURRENT_FOLDER("Unzip to current folder"),
        CLEAR_HISTORY("Clear history"),
        SYNC("Sync"),
        CREATE_FOLDER("Create folder"),
        TRASH("Trash"),
        EMPTY("Empty"),
        RESTORE("Restore"),
        CLOUD_USAGE("Cloud usage"),
        LARGE_FILES("Large files"),
        CUSTOM("Custom"),
        UNUSED_FILES("Unused files"),
        DUPLICATE_FILES("Duplicate files"),
        UNNECESSARY_FILES("Unnecessary files"),
        BACK_UP("Back up"),
        FILTER("Filter"),
        SELECT_SUB_HEADER("Select sub header"),
        OK("Ok"),
        CANCEL("Cancel"),
        DONE("Done"),
        MOVE_TO("Move to"),
        COPY_TO("Copy to"),
        EXPAND_PANEL("Expand panel"),
        COPY_CANCEL("Copy_Cancel"),
        MOVE_CANCEL("Move_Cancel"),
        DELETE_CANCEL("Cancel_deleting"),
        DOWNLOAD_CANCEL("Cancel downloading"),
        OPEN_SOURCE_LICENSES("Open source licenses"),
        REORDER("Reorder"),
        RESULT_EXPAND("Result expand"),
        SEARCH_FOLDER("Search folder"),
        SEARCH_FILE("Search file"),
        BACK_UP_TO("Back up to"),
        CHOOSE_FILES("Choose files"),
        UPDATE("Update My Files"),
        NETWORK_NOT_AVAILABLE("Network not available"),
        WIFI_NOT_AVAILABLE("Wi-Fi not available"),
        COULD_NOT_BACK_UP_FILES("Couldn't back up files"),
        MANAGE_WIFI("Manage WiFi"),
        SHOW_HIDDEN("Show hidden"),
        ABOUT("About"),
        SIGN_OUT("Sign out"),
        SELECT_ITEM("Select item"),
        ADD_ACCOUNT("Add account"),
        DOWNLOAD("Download"),
        RETRY("Retry"),
        SHOW_RECENT_FILES("Show recent files");

        private String mEventName;
        private int mCode;

        Event(String eventName) {
            mEventName = eventName;
            mCode = mEventCodeGenerator++;
        }

        public String getEventName() {
            return mEventName;
        }

        public int getCode() {
            return mCode;
        }
    }

    private static int mSelectModeCodeGenerator = 0;

    public enum SelectMode {
        NORMAL,
        SELECTION_MODE,
        SELECTION_CATEGORY_FOLDER_MODE;

        private int mCode;

        SelectMode() {
            mCode = mSelectModeCodeGenerator++;
        }

        public int getCode() {
            return mCode;
        }
    }

    private static int mScreenPathCodeGenerator = 0;

    public enum ScreenPath {
        HOME,
        RECENT_FILES,
        CATEGORY_IMAGE,
        CATEGORY_VIDEO,
        CATEGORY_AUDIO,
        CATEGORY_DOCUMENT,
        DOWNLOADS,
        INSTALLATION,
        INTERNAL_STORAGE,
        SD_CARD,
        USB_STORAGE,
        SAMSUNG_CLOUD_DRIVE,
        SAMSUNG_DRIVE_TRASH,
        GOOGLE_CLOUD_DRIVE,
        SEARCH,
        MOVE,
        COPY,
        POPUP,
        SORTBY_POPUP,
        CREATE_FOLDER_POPUP,
        RENAME_POPUP,
        SHARE_FOLDER_AND_FILES_POPUP,
        UNZIP_POPUP,
        ZIP_POPUP,
        DELETE_POPUP,
        RENAME_SPECIAL_CHARACTER_POPUP,
        REPLACE_POPUP,
        DETAILS_POPUP,
        UPDATE_POPUP,
        NETWORK_NOT_AVAILABLE_POPUP,
        WIFI_NOT_AVAILABLE_POPUP,
        NOT_ENOUGH_STORAGE_POPUP,
        MAXIMUM_ITEM_REACHED,
        FILE_NOT_OPEN_POPUP,
        GET_MORE_SPACE, // ID:191
        GET_MORE_SPACE_LARGE, // ID:192
        GET_MORE_SPACE_LARGE_BACK_UP, // ID:192
        GET_MORE_SPACE_LARGE_POP_WIFI_NOT_AVAILABLE, // ID:192
        GET_MORE_SPACE_LARGE_POP_COULD_NOT_BACK_UP_FILES, // ID:192
        GET_MORE_SPACE_UNUSED, // ID:193
        GET_MORE_SPACE_UNUSED_BACK_UP, // ID:192
        GET_MORE_SPACE_UNUSED_POP_WIFI_NOT_AVAILABLE, // ID:193
        GET_MORE_SPACE_UNUSED_POP_COULD_NOT_BACK_UP_FILES, // ID:193
        GET_MORE_SPACE_DUPLICATE, // ID:194
        GET_MORE_SPACE_UNNECESSARY, // ID:195
        GET_MORE_SPACE_LARGE_POP_DELETE_FILES, // ID:192
        GET_MORE_SPACE_UNUSED_POP_DELETE_FILES, // ID:193
        GET_MORE_SPACE_DUPLICATE_POPUP_DELETE_FILES, //ID:194
        GET_MORE_SPACE_UNNECESSARY_POPUP_DELETE_FILES, // ID:195
        MANAGE_SHORTCUT,
        SETTINGS,
        SETTINGS_SAMSUNG_CLOUD_ACCOUNT,
        SETTINGS_GOOGLE_CLOUD_ACCOUNT,
        SETTINGS_GET_MORE_LARGE,
        SETTINGS_GET_MORE_UNUSED,
        SETTINGS_ABOUT,
        SETTINGS_OPEN_SOURCE_LICENCES,
        FROM_OTHER,
        FROM_OTHER_SINGLE,
        FROM_INTERNET;

        private int mCode;

        ScreenPath() {
            mCode = mScreenPathCodeGenerator++;
        }

        public int getCode() {
            return mCode;
        }
    }

    public static String getVersion() {
        return sVersion;
    }

    public static String getTrackingId() {
        return sTrackingId;
    }

    public static void setAutoLogging() {
    }

    private static void addEventLoggingInfo(String id, Event event) {
        registerEventLoggingInfo(id, getEventLoggingCode(event));
    }

    private static void addEventLoggingInfo(String id, Event event, ScreenPath screenPath) {
        registerEventLoggingInfo(id, getEventLoggingCode(event, screenPath));
    }

    private static void addEventLoggingInfo(String id, Event event, ScreenPath screenPath, SelectMode selectMode) {
        registerEventLoggingInfo(id, getEventLoggingCode(event, screenPath, selectMode));
    }

    private static void registerEventLoggingInfo(String id, int loggingCode) {
        if (mLoggingInfoEventIdMap.get(loggingCode) != null) {
            throw new IllegalArgumentException("Can't register event logging info for " + id + ". Info is already registered");
        }
        mLoggingInfoEventIdMap.put(loggingCode, id);
    }

    private static int getEventLoggingCode(Event event) {
        return getEventLoggingCode(event, null, null);
    }

    private static int getEventLoggingCode(Event event, ScreenPath screenPath) {
        return getEventLoggingCode(event, screenPath, null);
    }

    private static int getEventLoggingCode(Event event, ScreenPath screenPath, SelectMode selectMode) {
        int ret = (event.getCode() << 16);
        if (screenPath != null) {
            ret |= (screenPath.getCode() << 8);
        }
        if (selectMode != null) {
            ret |= selectMode.getCode();
        }
        return ret;
    }

    private static void addPageLoggingInfo(String id, ScreenPath screenPath) {
        registerPageLoggingInfo(id, getPageLoggingCode(screenPath));
    }

    private static void addPageLoggingInfo(String id, ScreenPath screenPath, SelectMode selectMode) {
        registerPageLoggingInfo(id, getPageLoggingCode(screenPath, selectMode));
    }

    private static void registerPageLoggingInfo(String id, int loggingCode) {
        if (mLoggingInfoPageIdMap.get(loggingCode) != null) {
            throw new IllegalArgumentException("Can't register page logging info for " + id + ". Info is already registered");
        }
        mLoggingInfoPageIdMap.put(loggingCode, id);
    }

    private static int getPageLoggingCode(ScreenPath screenPath) {
        return getPageLoggingCode(screenPath, null);
    }

    private static int getPageLoggingCode(ScreenPath screenPath, SelectMode selectMode) {
        if (screenPath == null) {
            return 0;   // NEED TO CHECK THIS CONDITION
        }

        int ret = (screenPath.getCode() << 8);
        if (selectMode != null) {
            ret |= selectMode.getCode();
        }
        return ret;
    }

    private static ScreenPath findScreenPath(int processId) {
        NavigationInfo curInfo = NavigationManager.getInstance(processId).getCurInfo();
        ScreenPath screenPath = null;
        if (curInfo != null) {
            switch (curInfo.getStorageType()) {
                case Trash:
                    screenPath = ScreenPath.SAMSUNG_DRIVE_TRASH;
                    break;
                default:
                    screenPath = curInfo.getScreenPath();
                    break;
            }
        }
        return screenPath;
    }

    public static void sendLog(int processId, Event event, SelectMode selectMode) {

    }

    public static void sendLog(int processId, Event event, String detail, SelectMode selectMode) {

    }

    public static void sendLog(int processId, Event event, Long value, String detail, SelectMode selectMode) {

    }

    public static void sendLog(ScreenPath noNaviScreenPath, Event event, SelectMode selectMode) {

    }

    public static void sendLog(ScreenPath noNaviScreenPath, Event event, Long value, SelectMode selectMode) {

    }

    public static void sendLog(ScreenPath noNaviScreenPath, Event event, String detail, SelectMode selectMode) {

    }

    public static void sendLog(ScreenPath noNaviScreenPath, Event event, Long value, String detail, SelectMode selectMode) {

    }

    public static long getTotalSize(ArrayList<FileRecord> selectedFiles) {
        long totalSize = 0;
        for (FileRecord file : selectedFiles) {
            totalSize += (file != null) ? file.getSize() : 0;
        }
        return totalSize;
    }

    public static String getCountOfExtensions(ArrayList<FileRecord> selectedFiles) {
        HashMap<String, Integer> countOfExtensions = new HashMap<>();
        for (FileRecord file : selectedFiles) {
            String ext = file.getExt();
            int count = 1;
            if (countOfExtensions.containsKey(ext)) {
                count += countOfExtensions.get(ext);
            }
            countOfExtensions.put(ext, count);
        }
        return countOfExtensions.toString();
    }

    public static ScreenPath setGetMoreSpaceScreenPath(ScreenPath current) {
        if (current == null) return null;

        ScreenPath newScreenPath;
        if (current.getCode() == ScreenPath.GET_MORE_SPACE_LARGE_BACK_UP.getCode()
                || current.getCode() == ScreenPath.GET_MORE_SPACE_LARGE.getCode()) {
            newScreenPath = ScreenPath.GET_MORE_SPACE_LARGE_POP_DELETE_FILES;
        } else if (current.getCode() == ScreenPath.GET_MORE_SPACE_UNUSED_BACK_UP.getCode()
                || current.getCode() == ScreenPath.GET_MORE_SPACE_UNUSED.getCode()) {
            newScreenPath = ScreenPath.GET_MORE_SPACE_UNUSED_POP_DELETE_FILES;
        } else if (current.getCode() == ScreenPath.GET_MORE_SPACE_DUPLICATE.getCode()) {
            newScreenPath = ScreenPath.GET_MORE_SPACE_DUPLICATE_POPUP_DELETE_FILES;
        } else if (current.getCode() == ScreenPath.GET_MORE_SPACE_UNNECESSARY.getCode()) {
            newScreenPath = ScreenPath.GET_MORE_SPACE_UNNECESSARY_POPUP_DELETE_FILES;
        } else {
            newScreenPath = current;
        }
        return newScreenPath;
    }

    public static boolean isGetMoreSpaceDeleteScreenPath(ScreenPath current) {
        if (current == null) return false;
        return (current.getCode() >= ScreenPath.GET_MORE_SPACE_LARGE_POP_DELETE_FILES.getCode()
                && current.getCode() <= ScreenPath.GET_MORE_SPACE_UNNECESSARY_POPUP_DELETE_FILES.getCode());
    }
}
